package com.rpframework.utils

/**
 * 命名相关工具类
 * Created by lx178 on 2016/12/14.
 */
object NamedAfterUtil {

    /**
     * 驼峰命名[name]转成下划线[@return] eg: userName -> user_name
     */
    fun humpToUnderline(name: String): String {
        var index = arrayOf(0)
        name.forEachIndexed { i, c ->
            if (c.isUpperCase())
                index = index.plus(i)
        }
        index = index.plus(name.length)

        val sb = StringBuffer()
        index.forEachIndexed { i, v ->
            try {
                sb.append(name.substring(v, index[i + 1]).toLowerCase())
                sb.append("_")
            } catch (e: ArrayIndexOutOfBoundsException) {
                log.debug(e.message)
            }
        }
        return sb.toString().removeSuffix("_")
    }
}
